/**
* @Author: Rain
* @Date: 2019-11-20 14:56:20
* @LastEditTime: 2019-11-22 15:10:25
* @Description: Do Something
 */
var Common = require('../../utils/common.js');
import create from '../../utils/create2.js'
import store from '../../store/index.js'
import { JxbHttpClient} from "../../data/session/session"
var App = getApp();
create(store, {
  data: {
    tabList: [{ label: '未使用', value: 1, total: 0 }, { label: '已使用', value: 2, total: 0 }, { label: '已过期', value: 3, total: 0 }],
    list: [],
    coupon: {},
    type: 1,
    filter: false
  },
  onLoad: function (options) {
    const { from ,dinnerId } = options // from页面的进入点, 0:个人中心
    this.setData({
      filter: from ? true : false,
      dinnerId
    })
    if (from) {
      this.queryTotal()
    }
    this.getCouponList()
  },
  onShow(){
    getApp().getSaleMode()
  },
  getCouponList() {
    const { type, filter, dinnerId } = this.data
    let data = filter ? { type } : { type: 1 }
    Object.assign(data, dinnerId ? { dr_id: dinnerId } : {})
    new JxbHttpClient().request({
      url: 'AppMaster/BookDinner/my_dinner_coupon_list',
      data
    }).then(_ => { 
        const { result, data } = _._data
        if (result * 1 === 0) {
          this.setData({
            list: data.list
          })
        }
    })
  },
  useCoupon(e) {
    const { type } = this.data
    if (type * 1 > 1) return
    const { filter } = this.data
    const { index, op } = e.currentTarget.dataset
    const { list, coupon } = this.data
    const item = list[index]
    if (item.dinner_coupon_id === coupon.dinner_coupon_id && op * 1 === 2) {
      this.store.data.coupon = {}
      this.update()
      return
    } else {
      this.store.data.coupon = Object.assign({}, item)
      this.update()
      filter ? wx.navigateTo({
        url: `/pages/index/food/index?from=coupon`
      }) : wx.navigateBack()
    }
  },
  queryTotal() {
    Common.my_request({
      url: '/AppMaster/BookDinner/my_dinner_coupon_type_count',
      data: {},
      success: (res) => {
        const { result, data } = res.data
        if (result * 1 === 0) {
          let { tabList } = this.data
          tabList[0]['total'] = data.not_use_and_not_timeout_count
          tabList[1]['total'] = data.had_use_count
          tabList[2]['total'] = data.not_use_and_had_timeout_count
          this.setData({
            tabList
          })
        }
      }
    })
  },
  // 选项卡点击
  getCoupon: function (e) {
    var id = e.currentTarget.id;
    this.setData({
      type: id
    })
    this.getCouponList()
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  }
})